package com.iterator.service.web.marketManager;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iterator.bean.web.lc.BrandBean.BrandCreateBO;
import com.iterator.bean.web.lc.BrandBean.BrandUpdateBO;
import com.iterator.bean.web.lc.BrandBean.BrandVO;
import com.iterator.bean.web.lc.BrandBean.BrandListBO;
import com.iterator.bean.web.lc.BrandBean.BrandListVO;
import com.iterator.bean.web.pojo.Brand;
import com.iterator.bean.web.pojo.BrandExample;
import com.iterator.mapper.web.marketManager.BrandMapper;
import com.iterator.mapper.web.marketManager.CustomBrandMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;


import java.math.BigDecimal;
import java.util.Date;
import java.util.List;


@Service
public class BrandServiceImpl implements BrandService{
    @Autowired
    BrandMapper brandMapper;
    @Autowired
    CustomBrandMapper customBrandMapper;

    /**
     * 查询Brand
     * @param brandListBO
     * @return
     */
    @Override
    public BrandListVO brandList(BrandListBO brandListBO) {
        PageHelper.startPage(brandListBO.getPage(),brandListBO.getLimit());
        BrandExample brandExample = new BrandExample();
        brandExample.setOrderByClause(brandListBO.getSort() + " " + brandListBO.getOrder());
        BrandExample.Criteria criteria = brandExample.createCriteria();
        if(!StringUtils.isEmpty(brandListBO.getName())){
            criteria.andNameLike("%"+brandListBO.getName()+"%");
        }
        if(!StringUtils.isEmpty(brandListBO.getId())){
            criteria.andIdEqualTo(brandListBO.getId());
        }

        PageInfo<Brand> brandPageInfo = new PageInfo<>();
        long total = brandPageInfo.getTotal();

        List<Brand> brands = brandMapper.selectByExample(brandExample);

        BrandListVO brandListVO = new BrandListVO();
        brandListVO.setTotal((int) total);
        brandListVO.setItems(brands);
        return brandListVO;
    }

    /**
     * 创建Brand
     * @param createBO
     * @return
     */
    @Override
    public BrandVO createBrand(BrandCreateBO createBO) {
        Date date = new Date();
        Brand brand = new Brand(null,
                createBO.getName(),
                createBO.getDesc(),
                createBO.getPicUrl(),
                null,
                new BigDecimal(createBO.getFloorPrice()),
                date,
                date,
                false);
        Integer id = brandMapper.insertSelective(brand);
        BrandVO createVO = new BrandVO(id,createBO.getName(),createBO.getDesc(),
                new BigDecimal(createBO.getFloorPrice()),date,date);
        createVO.setAddTime(date);
        createVO.setUpdateTime(date);
        return createVO;
    }

    @Override
    public BrandVO update(BrandUpdateBO updateBO) {
        BrandExample example = new BrandExample();
        BrandExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(updateBO.getId());
        Date date = new Date();
        Brand brand = new Brand(updateBO.getId(),
                updateBO.getName(),
                updateBO.getDesc(),
                updateBO.getPicUrl(),
                null,
                new BigDecimal(updateBO.getFloorPrice()),
                null,
                date,
                false);
        int i = customBrandMapper.updateByExample(brand, example);
        BrandVO brandVO = new BrandVO(brand.getId(), brand.getName(), brand.getDesc(), brand.getFloorPrice(),
                brand.getAddTime(), date);
        return  brandVO;
    }

    @Override
    public Integer delete(Integer id) {
        BrandExample example = new BrandExample();
        BrandExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(id);
        Brand brand = new Brand();
//        只是将deleted设为true，不真正删除
        brand.setDeleted(true);
        return brandMapper.updateByExampleSelective(brand,example);
    }
}
